import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 * The main Screen for this game
 */
public class GameBoard extends Canvas {
	public Command no, yes;
	private Player me, them, winner, current_turn;
	private int[][] img_positions = { { 21, 21 }, { 88, 21 }, { 156, 21 },
			{ 21, 91 }, { 88, 91 }, { 156, 91 }, { 21, 162 }, { 88, 162 },
			{ 156, 162 } };
	private int dispWidth, dispHeight, left_x, right_x, top_y, bottom_y, num_moves;
	public Player[] grid;

	/**
	 * Construct the GameFrame object
	 */
	public GameBoard(Player m, Player t) {
		me = m;
		them = t;
		grid = new Player[9];

		for (int i = 0; i < grid.length; i++)
			grid[i] = null;

		no = new Command("No", Command.EXIT, 1);
		yes = new Command("Yes", Command.OK, 0);

		dispWidth = getWidth() - 20;
		dispHeight = getHeight() - 30;
		left_x = getWidth() / 3 + 5;
		right_x = getWidth() / 3 * 2 - 5;
		top_y = getHeight() / 3 + 5;
		bottom_y = getHeight() / 3 * 2 - 5;
		num_moves = 0;

		current_turn = me;
	}

	public void keyPressed(int keyCode) {
		int indx = -1;
		if (keyCode >= KEY_NUM1 && keyCode <= KEY_NUM9)
			indx = keyCode - KEY_NUM1;

		if (indx > -1 && checkMove(indx)) {
			String cmd_str = MyRMS.encrypt_cmd(indx+1, num_moves);
			int dec_int = MyRMS.decrypt_cmd(cmd_str, num_moves);
			System.out.println("ENCODED DATA: "+cmd_str);
			System.out.println("DECODED DATA: "+dec_int);
			MyRMS.send_cmd(cmd_str.getBytes());
			makeMove(indx, current_turn);
			this.repaint();
		}
	}

	public boolean checkMove(int indx) {
		if (hasWinner() || isStalemate() || grid[indx] != null)
			return false;
		return true;
	}

	public void makeMove(int indx, Player p) {
		if (!checkMove(indx))
			return;

		grid[indx] = p;
		num_moves++;
		if (current_turn.isWinner()) {
			winner = current_turn;
			showWinScreen();
		} else {
			current_turn = (current_turn == me ? them : me);
			setTitle(current_turn.getName() + "'s turn");
		}
	}

	public Player getWinner() {
		return winner;
	}

	public boolean hasWinner() {
		return winner != null;
	}

	public boolean isStalemate() {
		for (int i = 0; i < grid.length; i++)
			if (grid[i] == null)
				return false;
		return true;
	}

	public void showWinScreen() {

	}

	/**
	 * Clear the game screen
	 */
	private void clear(Graphics g) {
		g.setColor(0xFFFFFF);
		g.fillRect(0, 0, getWidth(), getHeight());
	}

	/**
	 * Paint the game screen
	 */
	public void paint(Graphics g) {
		synchronized (g) {
			// Clear screen
			clear(g);

			// Draw the grid
			g.setColor(0x000000);
			g.drawLine(left_x, 25, left_x, dispHeight);
			g.drawLine(right_x, 25, right_x, dispHeight);
			g.drawLine(20, top_y, dispWidth, top_y);
			g.drawLine(20, bottom_y, dispWidth, bottom_y);

			for (int i = 0; i < grid.length; i++) {
				if (grid[i] != null)
					g.drawImage(grid[i].getImage(), img_positions[i][0],
							img_positions[i][1], Graphics.TOP | Graphics.LEFT);
			}

			if (hasWinner() || isStalemate()) {
				g.setColor(255, 0, 0);
				g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
						Font.SIZE_SMALL));
				g.drawString("Play another game?", 0, 0, Graphics.TOP
						| Graphics.LEFT);
				this.addCommand(no);
				this.addCommand(yes);

				if (hasWinner())
					setTitle(winner.getName() + " wins!");
				else
					setTitle("Game is tied!");
			} else
				setTitle(current_turn.getName() + "'s turn...");
		}
	}
}